<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

require('echarts/theme/macarons') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    },
    name: {
      type: Array,
      required: true
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      let chartData = this.chartData;
      if (chartData.length > 0) {
        this.setOptions(this.chartData)
      }
    },
    setOptions({expectedData, actualData} = {}) {
      /* let name = this.name == "" ? "" : eval('(' + this.name + ')');
       let chartData = expectedData == "" ? "" : eval('(' + expectedData + ')');*/
      let name = this.name;
      let chartData = this.chartData;
      let rotate = 0;
      switch (name) {
        case name.length < 5:
          rotate = 0;
          break;
        case name.length >= 5 && name.length < 12:
          rotate = 0;
          break;
        case name.length >= 12:
          rotate = 0 - name.length * 5;
          break;
      }
      this.chart.setOption({
        xAxis: {
          // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          // data:['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          data: name,
          // data: this.name,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 0,
            // rotate: name.length < 5 ? 0 : 0 - name.length * 4
            rotate: -90
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          // data: ['expected', 'actual']
          // data: ['数量']
        },
        series: [{
          name: chartData[1].name,
          itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: chartData[1].data,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
          {
            name: chartData[0].name,
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: chartData[0].data,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }
          /*{
            name: 'actual',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: actualData,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }*/]
      })
    }
  }
}
</script>
